package com.zcfeng.ischool;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;

import java.util.HashMap;
import java.util.Iterator;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import socket.SocketRunnable;
import utils.MyConstaints;
import utils.ToastUtils;

/**
 * Created by Administrator on 2017/3/12.
 */

public class MyApp extends Application {
    private String str="";
    SocketRunnable socket;
    private HashMap<Integer,Handler>  handlerHashMap =new HashMap<>();
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==MyConstaints.socket_errer)ToastUtils.safeToast(getApplicationContext(),"socket连接失败");
                Iterator iter = handlerHashMap.entrySet().iterator();
                while (iter.hasNext()) {
                    HashMap.Entry entry = (HashMap.Entry) iter.next();

                    int key = (int) entry.getKey();
                    if (key == msg.what) {
                        Handler val = (Handler) entry.getValue();
                        Message newmsg=new Message();
                        newmsg.what=msg.what;
                        newmsg.obj=msg.obj;
                        val.sendMessage(newmsg);
                    }
                }

        }
    };
    Handler syshandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            str=msg.obj.toString();

           // ToastUtils.safeToast(getApplicationContext(),str);
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    SystemClock.sleep(1000);
//                    System.exit(0);
//                }
//            }).start();

        }
    };
    public void addHandle(int what,Handler hand){
                handlerHashMap.put(what,hand);
    }
    public SocketRunnable getSocket(){
        return socket;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        addHandle(MyConstaints.system,syshandler);
        socket=new SocketRunnable(handler);
        new Thread(socket).start();
        RongIM.init(this);
        RongIM.setOnReceiveMessageListener(new RongIMClient.OnReceiveMessageListener() {
            @Override
            public boolean onReceived(io.rong.imlib.model.Message message, int i) {
                return false;
            }
        });
    }
}
